package com.ruoyi.front.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.front.domain.TAccount;
import com.ruoyi.front.service.ITAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户账户Controller
 *
 * @author juck
 * @date 2021-04-23
 */
@RestController
@RequestMapping("/account")
@Api(tags = "用户相关")
public class TAccountController extends BaseController {
    @Autowired
    private ITAccountService tAccountService;

    @PreAuthorize(hasPermi = "front-front:account:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询用户账户列表")
    public TableDataInfo list(TAccount tAccount) {
        startPage();
        List<TAccount> list = tAccountService.selectTAccountList(tAccount);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "front-front:account:export")
    @Log(title = "用户账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出用户账户列表")
    public void export(HttpServletResponse response, TAccount tAccount) throws IOException {
        List<TAccount> list = tAccountService.selectTAccountList(tAccount);
        ExcelUtil<TAccount> util = new ExcelUtil<TAccount>(TAccount.class);
        util.exportExcel(response, list, "用户账户数据");
    }

    /**
     * 获取用户账户详细信息
     */
    @PreAuthorize(hasPermi = "front-front:account:query")
    @GetMapping(value = "/{aId}")
    public AjaxResult getInfo(@PathVariable("aId") Long aId) {
        return AjaxResult.success(tAccountService.selectTAccountById(aId));
    }

    /**
     * 新增用户账户
     */
    @PreAuthorize(hasPermi = "front-front:account:add")
    @Log(title = "用户账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAccount tAccount) {
        return toAjax(tAccountService.insertTAccount(tAccount));
    }

    /**
     * 修改用户账户
     */
    @PreAuthorize(hasPermi = "front-front:account:edit")
    @Log(title = "用户账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAccount tAccount) {
        return toAjax(tAccountService.updateTAccount(tAccount));
    }

    /**
     * 删除用户账户
     */
    @PreAuthorize(hasPermi = "front-front:account:remove")
    @Log(title = "用户账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{aIds}")
    public AjaxResult remove(@PathVariable Long[] aIds) {
        return toAjax(tAccountService.deleteTAccountByIds(aIds));
    }
}
